package team.sun.sss.controller.login;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import team.sun.sss.dao.user.UserDao;
import team.sun.sss.service.user.UserServiceImpl;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    UserServiceImpl userService;
    @Autowired
    UserDao userDao;

    //跳转登录界面
    @RequestMapping("/index")
    public String index(){
        return "login";
    }

    //修改密码
    @RequestMapping("/update_pwd")
    public String updatePwd(int userId,String userPwd,HttpSession session){
        userDao.updatePwd(userId,userPwd);
        session.removeAttribute("loginInfo");
        return "login";
    }
    //政工密码修改界面
    @RequestMapping("/update_pwd_t")
    public String updatePwdT(){
        return "/updatepwd/staff_update_pwd";
    }

    @PostMapping("/login")

    public String login(Model model, String userId, String userPwd, HttpSession session){
        int result = userService.login(Integer.parseInt(userId),userPwd);

        session.setAttribute("loginInfo",userService.getLoginInfo(Integer.parseInt(userId),userPwd));

        switch(result){
            case 1://学生
                return "/student/student_welcome";
            case 2 ://教师
                return "/student/instructor_welcome";
            case 3 ://管理员
                return "/hardWork/staff_welcome";
            case 4 ://用人单位
                return "/hardWork/unit_welcome";
        }
        model.addAttribute("msg","账号或密码失败");

        return "login";
    }
}
